package com.millstein.tsinglog.mapper.biz;

import com.millstein.tsinglog.mapper.BaseMapper;
import com.millstein.tsinglog.model.pojo.Tag;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface TagMapper extends BaseMapper<Tag> {

    /**
     * server用，根据id查询标签
     * @param id
     * @return
     */
    @Select("SELECT `id`, `tag_name` tagName, `cid`, `description`, `sort`, `usable`, `status`, " +
            "`create_time` createTime, `update_time` updateTime FROM `biz_tag` " +
            "where `status` = 1 and `id` = #{id};")
    Tag selectTagByPrimaryKeyForServer(@Param("id") Long id);

    /**
     * web用，根据id查询标签
     * @param id
     * @return
     */
    @Select("SELECT `id`, `tag_name` tagName, `cid`, `description`, `sort`, `usable`, `status`, " +
            "`create_time` createTime, `update_time` updateTime FROM `biz_tag` " +
            "where `usable` = 1 and `status` = 1 and `id` = #{id};")
    Tag selectTagByPrimaryKeyForWeb(@Param("id") Long id);

    /**
     * 为前台提供分类及其文章数量的统计信息
     * @return
     */
    @Select("select t.id, t.tag_name, count(at.tid) count " +
            "from `biz_tag` t " +
            "left join `biz_article_tag` at on t.id = at.tid " +
            "left join `biz_article` a on at.aid = a.id " +
            "where t.usable = 1 and t.status = 1 and a.is_release = 1 and a.status = 1 " +
            "group by at.tid having count > 0 " +
            "order by sort asc ")
    List<Map<String, Object>> getTagListForPortal();
}
